#!/bin/bash
set -e
set -u
trap onexit INT
trap onexit TERM
trap onexit EXIT

SUCCESS=0
PID=-1

onexit()
{
	if [ $SUCCESS -eq 1 ]; then
		echo GREAT SUCCESS!
	else
		echo Some errors were encountered.
	fi
	if [ $PID -ne -1 ]; then
		kill -0 $PID >/dev/null 2>&1 && kill $PID
	fi
}

BIN=@CMAKE_RUNTIME_OUTPUT_DIRECTORY@
SSL=@VGL_USESSL@

$BIN/bmptest
echo

NOSHM=
if [ "`uname -s`" = "Darwin" ]; then
	# MIT-SHM doesn't work with Xvfb on OS X for some reason.
	NOSHM=-noshm
fi

which Xvfb >/dev/null 2>&1 || (
	echo Xvfb not found!
	exit 1
)
Xvfb :42 -screen 0 1920x1200x24 >/dev/null 2>&1 & PID=$!
echo Xvfb started as process $PID
sleep 2
DISPLAY=:42 $BIN/fbxtest $NOSHM -time 0.2
DISPLAY=:42 $BIN/fbxtest $NOSHM -time 0.2 -mt
DISPLAY=:42 $BIN/fbxtest $NOSHM -time 0.2 -pm
DISPLAY=:42 $BIN/fbxtest $NOSHM -time 0.2 -pm -mt
kill $PID
PID=-1
sleep 2

Xvfb :42 -screen 0 1024x768x24 >/dev/null 2>&1 & PID=$!
echo Xvfb started as process $PID
sleep 2
DISPLAY=:42 $BIN/fbxtest $NOSHM -time 0.2
DISPLAY=:42 $BIN/fbxtest $NOSHM -time 0.2 -mt
DISPLAY=:42 $BIN/fbxtest $NOSHM -time 0.2 -pm
kill $PID
PID=-1
sleep 2

Xvfb :42 -screen 0 1920x1200x8 -class PseudoColor >/dev/null 2>&1 & PID=$!
echo Xvfb started as process $PID
sleep 2
DISPLAY=:42 $BIN/fbxtest $NOSHM -time 0.2 -index
DISPLAY=:42 $BIN/fbxtest $NOSHM -time 0.2 -index -mt
DISPLAY=:42 $BIN/fbxtest $NOSHM -time 0.2 -index -pm
DISPLAY=:42 $BIN/fbxtest $NOSHM -time 0.2 -index -pm -mt
kill $PID
PID=-1
sleep 2

Xvfb :42 -screen 0 1024x768x8 -class PseudoColor >/dev/null 2>&1 & PID=$!
echo Xvfb started as process $PID
sleep 2
DISPLAY=:42 $BIN/fbxtest $NOSHM -time 0.2 -index
DISPLAY=:42 $BIN/fbxtest $NOSHM -time 0.2 -index -mt
DISPLAY=:42 $BIN/fbxtest $NOSHM -time 0.2 -index -pm
kill $PID
PID=-1
sleep 2

$BIN/nettest -server &
echo
sleep 2
$BIN/nettest -client localhost -t 0.2

if [ "$SSL" = "1" ]; then
	echo
	$BIN/nettest -server -ssl &
	sleep 2
	$BIN/nettest -client localhost -ssl -t 0.2
fi

SUCCESS=1
